{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Vector Quantization Example\n\n\nFace, a 1024 x 768 size image of a raccoon face,\nis used here to illustrate how `k`-means is\nused for vector quantization.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\n\n# Code source: Ga\u00ebl Varoquaux\n# Modified for documentation by Jaques Grobler\n# License: BSD 3 clause\n\nimport numpy as np\nimport scipy as sp\nimport matplotlib.pyplot as plt\n\nfrom sklearn import cluster\n\n\ntry:  # SciPy >= 0.16 have face in misc\n    from scipy.misc import face\n    face = face(gray=True)\nexcept ImportError:\n    face = sp.face(gray=True)\n\nn_clusters = 5\nnp.random.seed(0)\n\nX = face.reshape((-1, 1))  # We need an (n_sample, n_feature) array\nk_means = cluster.KMeans(n_clusters=n_clusters, n_init=4)\nk_means.fit(X)\nvalues = k_means.cluster_centers_.squeeze()\nlabels = k_means.labels_\n\n# create an array from labels and values\nface_compressed = np.choose(labels, values)\nface_compressed.shape = face.shape\n\nvmin = face.min()\nvmax = face.max()\n\n# original face\nplt.figure(1, figsize=(3, 2.2))\nplt.imshow(face, cmap=plt.cm.gray, vmin=vmin, vmax=256)\n\n# compressed face\nplt.figure(2, figsize=(3, 2.2))\nplt.imshow(face_compressed, cmap=plt.cm.gray, vmin=vmin, vmax=vmax)\n\n# equal bins face\nregular_values = np.linspace(0, 256, n_clusters + 1)\nregular_labels = np.searchsorted(regular_values, face) - 1\nregular_values = .5 * (regular_values[1:] + regular_values[:-1])  # mean\nregular_face = np.choose(regular_labels.ravel(), regular_values, mode=\"clip\")\nregular_face.shape = face.shape\nplt.figure(3, figsize=(3, 2.2))\nplt.imshow(regular_face, cmap=plt.cm.gray, vmin=vmin, vmax=vmax)\n\n# histogram\nplt.figure(4, figsize=(3, 2.2))\nplt.clf()\nplt.axes([.01, .01, .98, .98])\nplt.hist(X, bins=256, color='.5', edgecolor='.5')\nplt.yticks(())\nplt.xticks(regular_values)\nvalues = np.sort(values)\nfor center_1, center_2 in zip(values[:-1], values[1:]):\n    plt.axvline(.5 * (center_1 + center_2), color='b')\n\nfor center_1, center_2 in zip(regular_values[:-1], regular_values[1:]):\n    plt.axvline(.5 * (center_1 + center_2), color='b', linestyle='--')\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}